// +----------------------------------------------------------------------
// | 更新弹窗
// +----------------------------------------------------------------------

// 用法
// showDialog<Null>(
//   context: context, //BuildContext对象
//   barrierDismissible: false,
//   builder: (BuildContext context) {
//     return UpdateDialog(
//       link: 'https://www.baidu.com',
//       tips: '更新一堆乱七八糟的东西',
//       isNotForce: true, // 不是强更
//     );
//   },
// );

import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import '../common/global/global.dart';

class UpdateDialog extends Dialog {
  UpdateDialog({
    Key key,
    this.version,
    this.link,
    this.tips,
    this.isNotForce,
  }) : super(key: key);

  final String version;
  final String link;
  final String tips;
  final bool isNotForce;

  // 点返回键或阴影部分会做些什么...
  _onNavigationClickEvent(context) {
    if (isNotForce) {
      Navigator.of(context).pop();
    } else {
      showToast("必须更新才能继续哟");
    }
  }

  // 跳转连接
  launchUrl() async {
    if (await canLaunch(link)) {
      await launch(link);
    }
  }

  // 提示框中间的提示文字样式
  TextStyle _style1() {
    return TextStyle(
      color: col("909399"),
      fontSize: sp(34),
    );
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () => Future.value(isNotForce),
      child: Material(
        //创建透明层
        type: MaterialType.transparency, //透明类型
        child: Stack(
          fit: StackFit.expand,
          children: <Widget>[
            GestureDetector(onTap: () => _onNavigationClickEvent(context)),
            Center(
              //保证控件居中效果
              child: SizedBox(
                width: px(590),
                height: px(700),
                child: Container(
                  decoration: BoxDecoration(
                    image: DecorationImage(
                      image: AssetImage("assets/images/version.png"),
                      fit: BoxFit.contain,
                    ),
                  ),
                  child: Stack(
                    children: <Widget>[
                      Positioned(
                        top: 0,
                        right: px(5),
                        child: Container(
                          width: px(80),
                          height: px(80),
                          child: FlatButton(
                            onPressed: () => _onNavigationClickEvent(context),
                            padding: EdgeInsets.zero,
                            child: Image.asset(
                              "assets/images/icon/cha.png",
                              width: px(30),
                              height: px(30),
                            ),
                          ),
                        ),
                      ),
                      Container(
                        width: w100(context),
                        child: Column(
                          mainAxisAlignment: MainAxisAlignment.spaceBetween,
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: <Widget>[
                            Column(
                              children: <Widget>[
                                Container(
                                  margin: EdgeInsets.only(top: px(335)),
                                  child: Text(
                                    '发现新版本',
                                    style: TextStyle(
                                      color: col("303133"),
                                      fontSize: sp(38),
                                    ),
                                  ),
                                ),
                                Container(
                                  width: w100(context),
                                  height: px(180),
                                  margin: EdgeInsets.only(
                                    top: px(20),
                                    left: px(50),
                                    right: px(50),
                                  ),
                                  child: ListView(
                                    children: <Widget>[
                                      Text(
                                        txt(tips),
                                        textAlign: TextAlign.justify,
                                        style: _style1(),
                                      ),
                                    ],
                                  ),
                                ),
                              ],
                            ),
                            Container(
                              width: w100(context),
                              height: px(95),
                              decoration: BoxDecoration(
                                border: Border(
                                  top: BorderSide(
                                    color: col("edeff2"),
                                    width: 1,
                                  ),
                                ),
                              ),
                              child: FlatButton(
                                onPressed: launchUrl,
                                child: Text(
                                  "立即体验",
                                  style: TextStyle(
                                    color: col("70cef9"),
                                    fontSize: px(38),
                                  ),
                                ),
                              ),
                            ),
                          ],
                        ),
                      )
                    ],
                  ),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
